package com.manualtrader.thread;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.table.DefaultTableModel;

import com.ctp.bean.MarketBean;
import com.ctp.controller.manager.TraderManager;
import com.ctp.socket.util.MarketUtil;
import com.jbooktrader.mytrader.Constant;
import com.jbooktrader.platform.marketbook.MarketBook;
import com.jbooktrader.platform.marketdepth.MarketDepth;
import com.jbooktrader.platform.trader.Trader;

public class ManualTradeThread extends Thread implements Runnable {

	private DefaultTableModel tModel = null;
	
	private Trader trader = null;

	public void setTableModel(DefaultTableModel tModel,Trader trader) {
		this.tModel = tModel;
		this.trader = trader;
	}

	public void run() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		Date enddate1,enddate2;
		int daynum1 = 0;
		int daynum2 = 0;
		try {
			enddate1 = dateFormat1.parse("2012-09-15");
			enddate2 = dateFormat1.parse("2012-12-15");
			daynum1 = (int) (getDaysBetween(enddate1));
			daynum2 = (int) (getDaysBetween(enddate2));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}

		while (true) {
			double price1 = trader.getAssistant().getMarketBook("XAGUSD").getMarketDepth().getMidPointPrice()*Constant.rmb*32.15;
			double price2 = trader.getAssistant().getMarketBook("AG1209").getMarketDepth().getMidPointPrice();
			double price3 = trader.getAssistant().getMarketBook("AG1212").getMarketDepth().getMidPointPrice();
			double price4 = trader.getAssistant().getMarketBook("XAUUSD").getMarketDepth().getMidPointPrice()*Constant.rmb/31.105;
			double price5 = trader.getAssistant().getMarketBook("AU1212").getMarketDepth().getMidPointPrice();
			double ask1 = trader.getAssistant().getMarketBook("XAGUSD").getMarketDepth().getAsks().get(0).getPrice();
			double ask2 = trader.getAssistant().getMarketBook("AG1209").getMarketDepth().getAsks().get(0).getPrice();
			double ask3 = trader.getAssistant().getMarketBook("AG1212").getMarketDepth().getAsks().get(0).getPrice();
			double ask4 = trader.getAssistant().getMarketBook("XAUUSD").getMarketDepth().getAsks().get(0).getPrice();
			double ask5 = trader.getAssistant().getMarketBook("AU1212").getMarketDepth().getAsks().get(0).getPrice();
			double bid1 = trader.getAssistant().getMarketBook("XAGUSD").getMarketDepth().getBids().get(0).getPrice();
			double bid2 = trader.getAssistant().getMarketBook("AG1209").getMarketDepth().getBids().get(0).getPrice();
			double bid3 = trader.getAssistant().getMarketBook("AG1212").getMarketDepth().getBids().get(0).getPrice();
			double bid4 = trader.getAssistant().getMarketBook("XAUUSD").getMarketDepth().getBids().get(0).getPrice();
			double bid5 = trader.getAssistant().getMarketBook("AU1212").getMarketDepth().getBids().get(0).getPrice();
			
			tModel.setValueAt(price1, 0, 1);
			tModel.setValueAt(price2, 1, 1);
			tModel.setValueAt(price3, 2, 1);
			tModel.setValueAt(price4, 3, 1);
			tModel.setValueAt(price5, 4, 1);
			
			
			tModel.setValueAt(bid1, 0, 2);
			tModel.setValueAt(bid2, 1, 2);
			tModel.setValueAt(bid3, 2, 2);
			tModel.setValueAt(bid4, 3, 2);
			tModel.setValueAt(bid5, 4, 2);
			
			tModel.setValueAt(ask1, 0, 3);
			tModel.setValueAt(ask2, 1, 3);
			tModel.setValueAt(ask3, 2, 3);
			tModel.setValueAt(ask4, 3, 3);
			tModel.setValueAt(ask5, 4, 3);

			tModel.setValueAt(price2-price1, 1, 4);
			tModel.setValueAt(price3-price1, 2, 4);
			tModel.setValueAt(price5-price4, 4, 4);		
			
			tModel.setValueAt(price3-price2, 2, 5);

			tModel.setValueAt(Math.log(price2/(price1+Constant.tddiff))*365*100/daynum1, 1, 6);
			tModel.setValueAt(Math.log(price3/(price1+Constant.tddiff))*365*100/daynum2, 2, 6);
			tModel.setValueAt(Math.log(price5/price4)*365*100/daynum2, 4, 6);
			
			tModel.setValueAt(Math.log(price3/price2)*365*100/(daynum2-daynum1), 2, 7);
			
			
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
			}
		}
	}
	
	public static double getDaysBetween(Date endDate) {  
		Date startDate = new Date(System.currentTimeMillis());
        Calendar fromCalendar = Calendar.getInstance();  
        fromCalendar.setTime(startDate);  
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);  
        fromCalendar.set(Calendar.MINUTE, 0);  
        fromCalendar.set(Calendar.SECOND, 0);  
        fromCalendar.set(Calendar.MILLISECOND, 0);  
  
        Calendar toCalendar = Calendar.getInstance();  
        toCalendar.setTime(endDate);  
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);  
        toCalendar.set(Calendar.MINUTE, 0);  
        toCalendar.set(Calendar.SECOND, 0);  
        toCalendar.set(Calendar.MILLISECOND, 0);  
  
        return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);  
    }  

}
